package xin.ryven.workflow.controller;

import org.camunda.bpm.engine.repository.ProcessDefinition;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import xin.ryven.workflow.dto.ProcessDefinitionDTO;
import xin.ryven.workflow.dto.ProcessDefinitionDeployDTO;
import xin.ryven.workflow.service.ProcessDefinitionService;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/api/process-definitions")
public class ProcessDefinitionController {

    private final ProcessDefinitionService processDefinitionService;

    public ProcessDefinitionController(ProcessDefinitionService processDefinitionService) {
        this.processDefinitionService = processDefinitionService;
    }

    @GetMapping
    public ResponseEntity<List<ProcessDefinitionDTO>> listProcessDefinitions() {
        List<ProcessDefinition> definitions = processDefinitionService.listProcessDefinitions();
        List<ProcessDefinitionDTO> dtos = definitions.stream()
                .map(def -> new ProcessDefinitionDTO(
                    def.getId(),
                    def.getKey(),
                    def.getName(),
                    def.getVersion(),
                    def.getDeploymentId()
                ))
                .collect(Collectors.toList());
        return ResponseEntity.ok(dtos);
    }

    @GetMapping("/xml")
    public ResponseEntity<String> getProcessDefinitionXml(@RequestParam("processId") String processId) {
        String xml = processDefinitionService.getProcessDefinitionXml(processId);
        return xml != null ? ResponseEntity.ok(xml) : ResponseEntity.notFound().build();
    }

    @PostMapping
    public ResponseEntity<Void> deployProcessDefinition(@RequestBody ProcessDefinitionDeployDTO dto) {
        try {
            processDefinitionService.deployProcessDefinition(dto.getName(), dto.getXml());
            return ResponseEntity.ok().build();
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
        }
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteProcessDefinition(@PathVariable String id, @RequestParam String processId) {
        try {
            processDefinitionService.deleteProcessDefinition(processId);
            return ResponseEntity.ok().build();
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
        }
    }

    @GetMapping("/{id}")
    public ResponseEntity<ProcessDefinitionDTO> getProcessDefinition(@PathVariable String id) {
        ProcessDefinition definition = processDefinitionService.getProcessDefinition(id);
        if (definition != null) {
            ProcessDefinitionDTO dto = new ProcessDefinitionDTO(
                definition.getId(),
                definition.getKey(),
                definition.getName(),
                definition.getVersion(),
                definition.getDeploymentId()
            );
            return ResponseEntity.ok(dto);
        }
        return ResponseEntity.notFound().build();
    }

    @GetMapping("/latest")
    public ResponseEntity<ProcessDefinitionDTO> getLatestByKey(@RequestParam String key) {
        ProcessDefinition definition = processDefinitionService.getLatestProcessDefinitionByKey(key);
        if (definition != null) {
            ProcessDefinitionDTO dto = new ProcessDefinitionDTO(
                definition.getId(),
                definition.getKey(),
                definition.getName(),
                definition.getVersion(),
                definition.getDeploymentId()
            );
            return ResponseEntity.ok(dto);
        }
        return ResponseEntity.notFound().build();
    }
} 